package cdu.bbs.bbsv2.controller;

import cdu.bbs.bbsv2.domain.Comment;
import cdu.bbs.bbsv2.domain.Topic;
import cdu.bbs.bbsv2.domain.User;
import cdu.bbs.bbsv2.service.CommentService;
import cdu.bbs.bbsv2.service.TopicService;
import cdu.bbs.bbsv2.service.UserService;
import cdu.bbs.bbsv2.utils.DateTimeUtil;
import cdu.bbs.bbsv2.utils.EmojiDealUtil;
import cdu.bbs.bbsv2.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private UserService userService;
    @Autowired
    private TopicService topicService;
    @Autowired
    CommentService commentService;

    //
    //@RequestMapping("/getTopics")
    //public void getTopics(HttpServletRequest request,HttpServletResponse response) {
    //    HttpSession session = request.getSession();
    //    // 未置顶的
    //    Map<Topic,User> topicUserMap = new LinkedHashMap<>();
    //    List<Topic> topics = topicService.getTopics();
    //    for (Topic topic : topics) {
    //        User user = userService.getUserById(topic.getCreateBy());
    //        topicUserMap.put(topic,user);
    //    }
    //
    //    // 置顶的
    //    Map<Topic,User> topicUserMapTop = new LinkedHashMap<>();
    //    List<Topic> topicsTop = topicService.getTopicsTop();
    //    for (Topic topic : topicsTop) {
    //        User user = userService.getUserById(topic.getCreateBy());
    //        topicUserMapTop.put(topic,user);
    //    }
    //
    //    session.setAttribute("topicUserMap",topicUserMap);
    //    session.setAttribute("topicUserMapTop",topicUserMapTop);
    //
    //    Map<String,Object> map = new HashMap<String, Object>();
    //    map.put("success",true);
    //    PrintJson.printJsonObj(response, map);
    //
    //}


    // 文章加精
    @RequestMapping("/essence")
    @ResponseBody
    public Map<String, Object> essence(String id) {
        Boolean flag = topicService.essence(id);
        Map<String,Object> map = new HashMap<>();
        map.put("success",flag);
        return map;
    }

    // 文章置顶
    @RequestMapping("/top")
    @ResponseBody
    public Map<String, Object> top(String id) {
        Boolean flag = topicService.top(id);
        Map<String,Object> map = new HashMap<>();
        map.put("success",flag);
        return map;
   }
    @RequestMapping("/topTopicByIds")
    @ResponseBody
    public Map<String ,Object> topTopicByIds(String[] ids )  {
        Boolean flag = topicService.topTopicByIds(ids);
        Map<String,Object> map = new HashMap();
        map.put("success",flag);

        return map;
    }

    @RequestMapping("/deleteTopicById")
    @ResponseBody
    public Map<String ,Object> deleteTopicById(String id )  {

        Boolean flag = topicService.deleteTopicById(id);
        Map<String,Object> map = new HashMap();
        map.put("success",flag);

        return map;
    }
    @RequestMapping("/deleteTopicByIds")
    @ResponseBody
    public Map<String ,Object> deleteTopicByIds(String[] ids )  {
        Boolean flag = topicService.deleteTopicByIds(ids);
        Map<String,Object> map = new HashMap();
        map.put("success",flag);

        return map;
    }

    //@RequestMapping("/pageList")
    //@ResponseBody
    //public Map<String, Object> pageList(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
    //                                    @RequestParam(value="pageSize",defaultValue = "5") int pageSize){
    //
    //    Map<Topic, User> topicsAndUser = topicService.getTopicAndUser(pageNum, pageSize);
    //    Map<Topic,User> topicUserMapTop =topicService.getTopTopicAndUser();
    //
    //    Map<String,Object> map = new HashMap();
    //    map.put("topicsAndUser",topicsAndUser);
    //    map.put("topicUserMapTop",topicUserMapTop);
    //
    //    return map;
    //}

    @RequestMapping("/collection")
    @ResponseBody
    public Map<String, Object> collection(String topicId,HttpSession session) {
        String userId = ((User)(session.getAttribute("user"))).getId();

        Topic topic = topicService.getTopicById(topicId);

        topic.setCollectionId(topic.getCollectionId()+","+userId);
        topic.setCollectionCount(topic.getCollectionCount()+1);

        Boolean flag = topicService.updateInfo(topic);
        if(flag)
            flag = topicService.insertTopicCollection(topicId,userId);

        Map<String,Object> map = new HashMap<>();
        map.put("success",flag);
        return map;

    }

    @RequestMapping("/uncollection")
    @ResponseBody

    public Map<String, Object> uncollection(String topicId,HttpSession session) {

        String userId = ((User)session.getAttribute("user")).getId();
        Topic topic = topicService.getTopicById(topicId);
        // todo 用户取消收藏
        topic.setCollectionId(topic.getCollectionId().replace((","+userId),""));

        topic.setCollectionCount(topic.getCollectionCount()-1);

        Boolean flag = topicService.updateInfo(topic);
        if (flag)
            flag = topicService.removeTopicCollection(topicId,userId);


        Map<String,Object> map = new HashMap<>();
        map.put("success",flag);
        return map;
    }

    @RequestMapping("/thumb")
    @ResponseBody

    public Map<String, Object> thumb(String topicId, HttpSession session) {
        // 点赞功能
        String userId = ((User)session.getAttribute("user")).getId();
        Topic topic = topicService.getTopicById(topicId);

        // todo 点赞
        topic.setThumbsId(topic.getThumbsId()+","+userId);

        topic.setThumbsCount(topic.getThumbsCount()+1);
        Boolean flag = topicService.updateInfo(topic);
        if (flag)
            topicService.insertTopicThumb(topicId,userId);

        Map<String,Object> map = new HashMap<>();
        map.put("success",flag);

        return map;
    }

    @RequestMapping("/unthumb")
    @ResponseBody
    public Map<String, Object> unthumb(String topicId, HttpSession session) {
        // 取消点赞功能

        String userId = ((User)session.getAttribute("user")).getId();
        Topic topic = topicService.getTopicById(topicId);

        // todo 取消点赞功能
        topic.setThumbsId(topic.getThumbsId().replace((","+userId),""));

        topic.setThumbsCount(topic.getThumbsCount()-1);

        Boolean flag = topicService.updateInfo(topic);
        if (flag)
            topicService.removeTopicThumb(topicId,userId);

        Map<String,Object> map = new HashMap<>();
        map.put("success",flag);
        return map;

    }

    @RequestMapping("getTopicById")
    public ModelAndView getTopicById(HttpServletRequest request) {

        String id= request.getParameter("id");

        Topic topic = topicService.getTopicById(id);
        String userId = ((User)(request.getSession().getAttribute("user"))).getId();
        User user = userService.getUserById(topic.getCreateBy());

        if(!userId.equals(topic.getCreateBy())){ // 观看用户不是发布者
            topic.setBrowseCount(topic.getBrowseCount()+1); // 浏览次数+1
            topicService.updateInfo(topic);
        }
        //获取当前帖子的用户的所有帖子topics集合
        List<Topic> topics = topicService.getTopicsByUserId(topic.getCreateBy());
        //帖子主人的帖子数量
        int topicCount =topicService.getUserTopicCount(topics);
        int collectionCount = topicService.geUserCollectionCount(topics);
        int thumbCount = topicService.getUserThumbCount(topics);

        // 获得该帖子的所有评论
        Map<Comment,User> commentUserMap = new LinkedHashMap<>();
        List<Comment> comments = commentService.getComments(id);
        for (Comment comment : comments) {
            User u = userService.getUserById(comment.getUserId());
            commentUserMap.put(comment,u);
        }

        Map<String,Object> map = new HashMap<>();

        map.put("topic",topic);
        map.put("user",user);
        map.put("commentUserMap",commentUserMap);
        //发布的帖子
        map.put("topicCount",topicCount);
        map.put("collectionCount",collectionCount);
        map.put("thumbCount",thumbCount);

        return new ModelAndView("topicInfo",map);
    }

    @RequestMapping("/releaseTopic")
    @ResponseBody
    public Map<String, Object> releaseTopic(HttpServletRequest request) {
        String id = UUIDUtil.getUUID();
        String title = request.getParameter("topicTitle");
        String content = request.getParameter("topicContent");
        String status = "00";

        String createTime = DateTimeUtil.getSysTime();
        String createBy = ((User) request.getSession().getAttribute("user")).getId();

        int browseCount = 0;
        int thumbsCount = 0;
        int commentCount = 0;
        int collectionCount = 0;

        String thumbsId = "";
        String collectionId = "";

        Topic topic = new Topic(id, title, EmojiDealUtil.changeEmojiToHtml(content), status, createTime,
                createBy, browseCount, thumbsCount, commentCount, collectionCount, thumbsId, collectionId);

        Boolean flag = topicService.addTopic(topic);
        Map<String,Object> map = new HashMap();

        map.put("success",flag);
        return map;

    }


}
